//
//  LancooEyeCareCoverWindow.m
//  LancooEyeCarePro
//
//  Created by dangwc on 2019/10/22.
//  Copyright © 2019 dangwc. All rights reserved.
//

#import "LancooEyeCareCoverWindow.h"

@implementation LancooEyeCareCoverWindow

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        //移除layer
        [self.layer.sublayers makeObjectsPerformSelector:@selector(removeFromSuperlayer)];
        //添加layer
        self.skinLayer = [LancooEyeCareCoverLayer layer];
        _skinLayer.delegate = self;
        _skinLayer.frame = CGRectMake(0,0,frame.size.width,frame.size.height);
        _skinLayer.backgroundColor = [UIColor blackColor].CGColor;
        _skinLayer.opacity = 0.5;
        [self.layer addSublayer:_skinLayer];
    }
    return self;
}


-(void)openAnimationFinished:(finishedBlock)block{
    CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnimation.delegate = self;
    opacityAnimation.fromValue = @(0);
    opacityAnimation.toValue = @(0.5);
    opacityAnimation.duration = 0.5;
    opacityAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    opacityAnimation.fillMode = kCAFillModeForwards;
    opacityAnimation.removedOnCompletion = NO;
    [self.skinLayer addAnimation:opacityAnimation forKey:@"openAnimation"];
    self.finishedBlock = block;
}


-(void)offAnimationFinished:(finishedBlock)block{
    CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnimation.delegate = self;
    opacityAnimation.fromValue = @(0.5);
    opacityAnimation.toValue = @(0);
    opacityAnimation.duration = 0.5;
    opacityAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    opacityAnimation.fillMode = kCAFillModeForwards;
    opacityAnimation.removedOnCompletion = NO;
    [self.skinLayer addAnimation:opacityAnimation forKey:@"offAnimation"];
    self.finishedBlock = block;
}


- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    self.finishedBlock();
}

@end
